package com.lzk.service.impl;

import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.servlet.http.HttpServletResponse;

import com.alibaba.druid.util.StringUtils;
import com.jfinal.kit.PropKit;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;
import com.lzk.controller.front.UserController;
import com.lzk.model.ZcData;
import com.lzk.model.ZcUser;
import com.lzk.service.UserService;
import com.lzk.util.MD5Util;
import com.lzk.util.Util;

public class UserServiceImpl implements UserService{

	public boolean checkUserName(String userName) {
		long count = Db.queryInt("SELECT COUNT(*) FROM `zc_user` WHERE `userName` = ?",userName);
		return count>0;
	}

	public boolean insertUser(ZcUser user) {
		user.setPwd(MD5Util.GetMD5Code(user.getPwd()));
		user.setCreateTime(Util.getNow());
		return user.save();
	}

	public boolean deleteUserById(int id) {	
		return ZcUser.dao.deleteById(id);
	}

	public ZcUser getUserById(int id) {
		ZcUser user=ZcUser.dao.findById(id);
		if(user==null) return null;
		user.setPwd("");//密码清空
		user.setHeadImg(PropKit.get("urlPath")+user.getHeadImg());//头像拼接虚拟路径
		return user;
	}
	
	public boolean editUser(ZcUser user,boolean isChange) {
		//先从数据库里根据id获取原数据，把需要修改的内容修改，再更新
		ZcUser oldUser = ZcUser.dao.findById(user.getId());
		if(oldUser==null) return false;
		oldUser.setUserStatus(user.getUserStatus());
		if (user.getHeadImg()!=null) {
			oldUser.setHeadImg(user.getHeadImg());
		}
		if (isChange) {
			oldUser.setPwd(MD5Util.GetMD5Code(user.getPwd()));
		}
		return oldUser.update();
	}

	public List<ZcUser> getUserListByConection(int page, int limit, String userName, String userStatus) {
		String where = " WHERE 1=1 ";
		if (!StringUtils.isEmpty(userName)) {
			where+=" AND `userName` LIKE '%" + userName + "%' ";
			if (!Util.sqlCheck(userName)) {
				return null;
			}
		}

		if (!StringUtils.isEmpty(userStatus)) {
			where+=" AND `userStatus` = " + userStatus;
			if (!Util.sqlCheck(userStatus)) {
				return null;
			}
		}

		Page<ZcUser> dbList = ZcUser.dao.paginate(page, limit, "SELECT * ","FROM `zc_user` "+where+" ORDER BY `createTime` DESC");
		List<ZcUser> list = dbList.getList();
		for (ZcUser user : list) {
			user.setPwd("");//密码清空
			user.setHeadImg(PropKit.get("urlPath")+user.getHeadImg());//头像拼接虚拟路径
		}
		return list;
	}

	public long getUserContByConection(String userName, String userStatus) {
		String where = " WHERE 1=1 ";
		if (!StringUtils.isEmpty(userName)) {
			where += " AND `userName` LIKE '%" + userName + "%' ";
			if (!Util.sqlCheck(userName)) {
				return 0;
			}
		}
		if (!StringUtils.isEmpty(userStatus)) {
			where += " AND `userStatus` = " + userStatus;
			if (!Util.sqlCheck(userStatus)) {
				return 0;
			}
		}
		return Db.queryLong("SELECT COUNT(*) FROM `zc_user` "+where);
	}

	@Override
	public boolean login(String userName, String pwd, HttpServletResponse resp) {
		ZcData rightStatus = ZcData.dao.findFirst("SELECT * FROM `zc_data` WHERE `key` = ? AND `value` = ?","userStatus","正常");
		ZcUser user = ZcUser.dao.findFirst("SELECT * FROM `zc_user` WHERE `userName` = ? AND `pwd` = ? AND `userStatus` = ?",userName,MD5Util.GetMD5Code(pwd),rightStatus.getValueId());
		if (user==null) {
			return false;
		}
		//写入cookie登录与否的信息 token令牌：随机字符 后端Redis-->前端Map
		String token = Util.getToken();
		UserController.tokenMap.put(token, user);
		Util.addCookie(resp, PropKit.get("cookieToken"), token, 7);
		return true;
	}

}
